package 剑指offer;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author zhangjunfei
 * @date 2023/8/31 16:47
 */
public class O_41 {

    Queue<Integer> a = new PriorityQueue<>();
    Queue<Integer> b = new PriorityQueue<>((x, y) -> y - x);

    public void addNum(int num) {
        if (a.size() != b.size()) {
            a.add(num);
            b.add(a.poll());
        }else {
            b.add(num);
            a.add(b.poll());
        }
    }

    public double findMedian() {
       return a.size()!=b.size() ? a.peek() : (a.peek() + b.peek()) / 2.0;
    }
}
